package organization

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
)

func (c *Controller) ActivityInsert() {
	name := c.GetString("name")
	introduction := c.GetString("introduction")

	activity := models.OrganizationActivity{
		Name:                name,
		Introduction:        introduction,
		OrganizationID:      c.OrganizationAdmin.OrganizationID,
		OrganizationAdminID: c.OrganizationAdminID,
	}

	if value := db.DB.Create(&activity); value.Error != nil {
		c.ResponseFailed(201, "新增活动失败")
		return
	}

	c.ResponseSuccess()
}

// 硬删除
func (c *Controller) ActivityDelete() {
	activityID, err := c.GetInt("activity_id")
	if err != nil {
		c.ResponseFailed(201, "activity_id 不合法")
		return
	}

	if value := db.DB.Unscoped().Delete(&models.OrganizationActivity{}, "id = ?", activityID); value.Error != nil {
		c.ResponseFailed(202, "删除用户失败")
		return
	}
	c.ResponseSuccess()
}

func (c *Controller) ActivityUpdate() {
	activityID, err := c.GetInt("activity_id")
	if err != nil {
		c.ResponseFailed(201, "activity_id 不合法")
		return
	}

	var activity models.OrganizationActivity
	if value := db.DB.Where("id = ?", activityID).First(&activity); value.Error != nil {
		c.ResponseFailed(202, "此活动不存在")
		return
	}

	if c.OrganizationAdminID != activity.OrganizationAdminID {
		c.ResponseFailed(203, "仅发布者可修改活动内容")
		return
	}

	name := c.GetString("name")
	introduction := c.GetString("introduction")
	updateValue := make(map[string]interface{})
	if name != "" {
		updateValue["name"] = name
	}
	if introduction != "" {
		updateValue["introduction"] = introduction
	}
	db.DB.Model(&models.OrganizationActivity{}).Where("id = ?", activityID).Updates(updateValue)

	c.ResponseSuccess()
}
